package com.liang.pcc.service;

import com.liang.pcc.domain.BusParam;
import com.liang.pcc.util.TreeNode;
import com.liang.pcc.util.TreeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class BusParamAsyncService {

    private final TopologyDataService topologyDataService;
    private final BusParamService busParamService;

    /**
     * 根据母线名称异步更新反向负载率和最小独立可装
     */
    public void asyncUpdateBusParams(Long fileId, Long userId) {
        log.info("开始异步更新母线参数");
        List<TreeNode> topologyData = topologyDataService.getTopologyData(fileId, userId);
        List<BusParam> busParams = TreeUtil.extractBusParams(topologyData);
        busParams.forEach(data -> {
            busParamService.lambdaUpdate()
                    .eq(BusParam::getBusName, data.getBusName())
                    .eq(BusParam::getFileId, fileId)
                    .eq(BusParam::getUserId, userId)
                    .set(BusParam::getMaxReverseLoadRate, data.getMaxReverseLoadRate())
                    .set(BusParam::getMinIndependentMountable, data.getMinIndependentMountable())
                    .update();
        });
    }
}